import { createApp } from 'vue'
import { createPinia } from 'pinia'
import router from './router'
import App from './App.vue'

// 样式
import './styles/main.css'

// 创建应用
const app = createApp(App)

// 使用插件
app.use(createPinia())
app.use(router)

// 全局错误处理
app.config.errorHandler = (error, _instance, info) => {
  console.error('Global error:', error)
  console.error('Error info:', info)
  
  // 可以在这里添加错误上报
  if (error instanceof Error) {
    // 显示用户友好的错误提示
    console.error('Error message:', error.message)
  }
}

// 全局属性
app.config.globalProperties.$filters = {
  formatDate: (date: string | Date, format = 'YYYY-MM-DD HH:mm:ss') => {
    if (!date) return ''
    
    const d = new Date(date)
    const year = d.getFullYear()
    const month = String(d.getMonth() + 1).padStart(2, '0')
    const day = String(d.getDate()).padStart(2, '0')
    const hours = String(d.getHours()).padStart(2, '0')
    const minutes = String(d.getMinutes()).padStart(2, '0')
    const seconds = String(d.getSeconds()).padStart(2, '0')
    
    return format
      .replace('YYYY', String(year))
      .replace('MM', month)
      .replace('DD', day)
      .replace('HH', hours)
      .replace('mm', minutes)
      .replace('ss', seconds)
  },
  
  formatMoney: (amount: number, currency = '¥') => {
    if (typeof amount !== 'number') return '-'
    return `${currency}${(amount / 100).toFixed(2)}`
  },
  
  formatPercent: (value: number, decimals = 1) => {
    if (typeof value !== 'number') return '-'
    return `${(value * 100).toFixed(decimals)}%`
  }
}

// 挂载应用
app.mount('#app')

// 热更新支持
if (import.meta.hot) {
  import.meta.hot.accept()
}